package com.egg.ylt.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.egg.ylt.R;
import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.GlideCircleTransform;
import com.egg.ylt.Utils.GlideRoundTransform;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.adapter.ADA_BabyList;
import com.egg.ylt.adapter.ADA_BabyRelation;
import com.egg.ylt.adapter.ADA_FamilyList;
import com.egg.ylt.adapter.ADA_SelectRelation;
import com.egg.ylt.adapter.RvSimpleAdapter;
import com.egg.ylt.pojo.BabyEntity;
import com.egg.ylt.pojo.MemberBabyEntity;
import com.egg.ylt.pojo.RelationEntity;
import com.egg.ylt.pojo.RelativeFamilyEntity;
import com.egg.ylt.presenter.impl.FamilyPresenterImpl;
import com.egg.ylt.view.IFamilyView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;
import com.yonyou.framework.library.adapter.rv.MultiItemTypeAdapter;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.base.BaseAppCompatActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.common.utils.DisplayUtil;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.common.utils.ScreenUtil;
import com.yonyou.framework.library.common.utils.ToastUtil;
import com.yonyou.framework.library.dialog.AlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by qiuzhenhuan on 2019/1/18.
 */

public class ACT_Family extends BaseActivity<FamilyPresenterImpl> implements IFamilyView {


    @BindView(R.id.ll_my_content)
    LinearLayout llMyContent;
    @BindView(R.id.ll_my_empty)
    LinearLayout llMyEmpty;
    @BindView(R.id.rl_include_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_selector)
    ImageView ivSelector;

    @BindView(R.id.tv_add_mother)
    TextView tvAddMother;
    @BindView(R.id.tv_remove_mother)
    TextView tvRemoveMother;
    @BindView(R.id.iv_mother_avator)
    ImageView ivMotherAvator;
    @BindView(R.id.tv_mother_name)
    TextView tvMotherName;

    @BindView(R.id.tv_add_father)
    TextView tvAddFather;
    @BindView(R.id.tv_remove_father)
    TextView tvRemoveFather;
    @BindView(R.id.iv_father_avator)
    ImageView ivFatherAvator;
    @BindView(R.id.tv_father_name)
    TextView tvFatherName;

    @BindView(R.id.iv_grandfa1)
    ImageView ivGrandfa1;
    @BindView(R.id.tv_grandfa1_name)
    TextView tvGrandfa1Name;
    @BindView(R.id.tv_invite_grandfa1)
    TextView tvInviteGrandfa1;
    @BindView(R.id.tv_remove_grandfa1)
    TextView tvRemoveGrandfa1;

    @BindView(R.id.iv_grandma1)
    ImageView ivGrandma1;
    @BindView(R.id.tv_grandma1_name)
    TextView tvGrandma1Name;
    @BindView(R.id.tv_invite_grandma1)
    TextView tvInviteGrandma1;
    @BindView(R.id.tv_remove_grandma1)
    TextView tvRemoveGrandma1;

    @BindView(R.id.iv_grandfa2)
    ImageView ivGrandfa2;
    @BindView(R.id.tv_grandfa2_name)
    TextView tvGrandfa2Name;
    @BindView(R.id.tv_invite_grandfa2)
    TextView tvInviteGrandfa2;

    @BindView(R.id.tv_remove_grandfa2)
    TextView tvRemoveGrandfa2;

    @BindView(R.id.iv_grandma2)
    ImageView ivGrandma2;
    @BindView(R.id.tv_grandma2_name)
    TextView tvGrandma2Name;
    @BindView(R.id.tv_invite_grandma2)
    TextView tvInviteGrandma2;
    @BindView(R.id.tv_remove_grandma2)
    TextView tvRemoveGrandma2;
    @BindView(R.id.view_pop)
    View viewPopBg;

    private PopupWindow babyListDialog;

    private AlertDialog inviteDialog;

    private AlertDialog bindRelativeDialog;

    private TextView mTvInviteTitle;

    private ADA_BabyList mBabyAdapter;

    private MemberBabyEntity.ListBean mSelectedBaby;

    private ADA_SelectRelation mAdapter;

    private boolean isAdmin = false;


    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
    }


    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.color_FFB024);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, rlTitle);

        initInviteDialog();

        initBabyListDialog();

        initBindRelativeDialog();

        updateInviteStatus(new HashMap<String, RelativeFamilyEntity.ListBean>());

        mPresenter.getBabyList();
        mPresenter.getRelationList();

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mPresenter.getBabyList();
    }

    @Override
    public void showFamilyList(RelativeFamilyEntity familyEntity) {
        HashMap<String, RelativeFamilyEntity.ListBean> relativeMap = new HashMap<>();
        if (!ListUtil.isListEmpty(familyEntity.getList())) {
            relativeMap.put("妈妈", null);
            relativeMap.put("爸爸", null);
            relativeMap.put("爷爷", null);
            relativeMap.put("奶奶", null);
            relativeMap.put("姥爷", null);
            relativeMap.put("姥姥", null);

            for (RelativeFamilyEntity.ListBean listBean : familyEntity.getList()) {
                if (relativeMap.containsKey(listBean.getRelationshipName())) {
                    relativeMap.put(listBean.getRelationshipName(), listBean);
                }
            }
        }
        updateInviteStatus(relativeMap);
    }

    @Override
    public void showBabyList(MemberBabyEntity entity) {
        if (ListUtil.isListEmpty(entity.getList())) {
            tvTitle.setText("亲友团");
            llMyContent.setVisibility(View.GONE);
            llMyEmpty.setVisibility(View.VISIBLE);
            ivSelector.setVisibility(View.GONE);
        } else {

            llMyContent.setVisibility(View.VISIBLE);
            llMyEmpty.setVisibility(View.GONE);
            mSelectedBaby = getDefaultBaby(entity);
            isAdmin = mSelectedBaby.isAdmin();
            tvTitle.setText(String.format("%s的亲友团", mSelectedBaby.getName()));
            if (entity.getList().size() > 1) {
                mSelectedBaby.setChecked(true);
                ivSelector.setVisibility(View.VISIBLE);
            } else {
                ivSelector.setVisibility(View.GONE);
            }
            mBabyAdapter.update(entity.getList());

            if (com.egg.ylt.Utils.StringUtil.isEmpty(mSelectedBaby.getRelationshipId())) {
                bindRelativeDialog.show();
                return;
            }

            mPresenter.loadFamilyInfo(mSelectedBaby.getId());

        }
    }

    private MemberBabyEntity.ListBean getDefaultBaby(MemberBabyEntity entity) {
        for (MemberBabyEntity.ListBean baby : entity.getList()) {
            if (baby.getId().equals(Constants.BABYID)) {
                return baby;
            }
        }
        return entity.getList().get(0);
    }

    @Override
    public void showLoadBabyError(String result) {
        tvTitle.setText("亲友团");
        llMyContent.setVisibility(View.GONE);
        llMyEmpty.setVisibility(View.VISIBLE);
        ivSelector.setVisibility(View.GONE);
        ToastUtil.makeText(mContext, result, false);
    }

    @Override
    public void showRemoveSuccess() {
        CommonUtils.makeEventToast(mContext, "移除成功", false);
        mPresenter.loadFamilyInfo(mSelectedBaby.getId());
    }

    @Override
    public void bindRelativeSuccess() {
        mPresenter.loadFamilyInfo(mSelectedBaby.getId());
        bindRelativeDialog.dismiss();
    }

    @Override
    public void updateRelationList(List<RelationEntity.ListBean> list) {
        mAdapter.update(list, true);
    }


    @Override
    public void showException(String msg) {
        CommonUtils.makeEventToast(mContext, msg, false);
    }

    @OnClick({R.id.tv_add_baby, R.id.include_iv_back, R.id.tv_add_mother, R.id.tv_add_father, R.id.tv_invite_grandfa1,
            R.id.tv_invite_grandfa2, R.id.tv_invite_grandma1, R.id.tv_invite_grandma2, R.id.iv_selector, R.id.view_pop,
            R.id.tv_remove_grandfa1, R.id.tv_remove_grandfa2, R.id.tv_remove_grandma1, R.id.tv_remove_grandma2,
            R.id.tv_remove_father, R.id.tv_remove_mother})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_baby:
                ACT_SelectBaby.startActResource(mContext, ACT_EditBabyInfo.START_FROM_RELATION);
                break;
            case R.id.include_iv_back:
                finish();
                break;
            case R.id.tv_add_mother:
                showInviteDialog("妈妈");
                break;
            case R.id.tv_add_father:
                showInviteDialog("爸爸");
                break;
            case R.id.tv_invite_grandfa1:
                showInviteDialog("爷爷");
                break;
            case R.id.tv_invite_grandma1:
                showInviteDialog("奶奶");
                break;
            case R.id.tv_invite_grandfa2:
                showInviteDialog("姥爷");
                break;
            case R.id.tv_invite_grandma2:
                showInviteDialog("姥姥");
                break;
            case R.id.iv_selector:
            case R.id.view_pop:
                if (babyListDialog.isShowing()) {
                    babyListDialog.dismiss();
                    viewPopBg.setVisibility(View.GONE);
                } else {
                    babyListDialog.showAsDropDown(rlTitle);
                    viewPopBg.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.tv_remove_grandfa1:
            case R.id.tv_remove_grandma1:
            case R.id.tv_remove_grandfa2:
            case R.id.tv_remove_grandma2:
            case R.id.tv_remove_father:
            case R.id.tv_remove_mother:
                removeRelative((RelativeFamilyEntity.ListBean) view.getTag());
                break;

        }
    }

    private void showInviteDialog(String inviteName) {
        mTvInviteTitle.setText(String.format("邀请%s加入", inviteName));
        mTvInviteTitle.setTag(inviteName);
        if (isAdmin) {
            inviteDialog.show();
        } else {
            CommonUtils.makeEventToast(mContext, "只有管理员才有邀请权限哦~", false);
        }
    }


    private void updateInviteStatus(HashMap<String, RelativeFamilyEntity.ListBean> relativeMap) {

        RelativeFamilyEntity.ListBean matherBean = relativeMap.get("妈妈");
        if (matherBean != null) {
            if (!StringUtil.isEmpty(matherBean.getType()) && matherBean.getType().equals("2")) {
                if (isAdmin) {
                    tvAddMother.setBackgroundResource(R.drawable.bg_invite_enable);
                } else {
                    tvAddMother.setBackgroundResource(R.drawable.bg_invite_unable);
                }
                tvAddMother.setVisibility(View.VISIBLE);
                tvRemoveMother.setVisibility(View.GONE);
                tvRemoveMother.setTag(null);
                tvMotherName.setText("未装机");
                tvMotherName.setTextColor(getResources().getColor(R.color.color_EF382D));
            } else {
                if (isAdmin) {
                    tvRemoveMother.setVisibility(mSelectedBaby.getRelationshipName().equals("妈妈") ? View.GONE :
                            View.VISIBLE);
                    tvRemoveMother.setTag(matherBean);
                    tvAddMother.setVisibility(View.GONE);
                    tvAddMother.setBackgroundResource(R.drawable.bg_invite_enable);
                } else {
                    tvAddMother.setBackgroundResource(R.drawable.bg_invite_unable);
                    tvAddMother.setVisibility(View.GONE);
                    tvRemoveMother.setVisibility(View.GONE);
                    tvRemoveMother.setTag(null);
                }
                tvMotherName.setText(StringUtil.isEmpty(matherBean.getMemberCompanyName()) ? "已邀请" :
                        matherBean.getMemberCompanyName());
                loadImageAvator(matherBean.getHeadImage(), R.mipmap.ic_mother, ivMotherAvator);
                tvMotherName.setTextColor(getResources().getColor(R.color.color_12CB77));
            }
        } else {
            if (isAdmin) {
                tvAddMother.setVisibility(View.VISIBLE);
                tvAddMother.setBackgroundResource(R.drawable.bg_invite_enable);
            } else {
                tvAddMother.setBackgroundResource(R.drawable.bg_invite_unable);
                tvAddMother.setVisibility(View.VISIBLE);
            }
            tvRemoveMother.setVisibility(View.GONE);
            tvRemoveMother.setTag(null);
            ivMotherAvator.setImageResource(R.mipmap.ic_mother);
            tvMotherName.setText("未邀请");
            tvMotherName.setTextColor(getResources().getColor(R.color.color_999999));

        }

        RelativeFamilyEntity.ListBean fatherBean = relativeMap.get("爸爸");
        if (fatherBean != null) {
            if (!StringUtil.isEmpty(fatherBean.getType()) && fatherBean.getType().equals("2")) {
                if (isAdmin) {
                    tvAddFather.setBackgroundResource(R.drawable.bg_invite_enable);
                } else {
                    tvAddFather.setBackgroundResource(R.drawable.bg_invite_unable);
                }
                tvAddFather.setVisibility(View.VISIBLE);
                tvRemoveFather.setVisibility(View.GONE);
                tvRemoveFather.setTag(null);
                tvFatherName.setText("未装机");
                tvFatherName.setTextColor(getResources().getColor(R.color.color_EF382D));

            } else {
                if (isAdmin) {
                    tvAddFather.setVisibility(View.GONE);
                    tvRemoveFather.setVisibility(mSelectedBaby.getRelationshipName().equals("爸爸") ? View.GONE :
                            View.VISIBLE);
                    tvRemoveFather.setTag(fatherBean);
                    tvAddFather.setBackgroundResource(R.drawable.bg_invite_enable);
                } else {
                    tvAddFather.setBackgroundResource(R.drawable.bg_invite_unable);
                    tvAddFather.setVisibility(View.GONE);
                    tvRemoveFather.setVisibility(View.GONE);
                    tvRemoveFather.setTag(null);
                }
                tvFatherName.setText(StringUtil.isEmpty(fatherBean.getMemberCompanyName()) ? "已邀请" :
                        fatherBean.getMemberCompanyName());
                tvFatherName.setTextColor(getResources().getColor(R.color.color_12CB77));
                loadImageAvator(fatherBean.getHeadImage(), R.mipmap.ic_father, ivFatherAvator);
            }
        } else {
            if (isAdmin) {
                tvAddFather.setVisibility(View.VISIBLE);
                tvAddFather.setBackgroundResource(R.drawable.bg_invite_enable);
            } else {
                tvAddFather.setBackgroundResource(R.drawable.bg_invite_unable);
                tvAddFather.setVisibility(View.VISIBLE);
            }
            tvRemoveFather.setVisibility(View.GONE);
            tvRemoveFather.setTag(null);
            ivFatherAvator.setImageResource(R.mipmap.ic_father);
            tvFatherName.setText("未邀请");
            tvFatherName.setTextColor(getResources().getColor(R.color.color_999999));
        }

        RelativeFamilyEntity.ListBean grandFatherBean1 = relativeMap.get("爷爷");
        if (grandFatherBean1 != null) {
            loadImageAvator(grandFatherBean1.getHeadImage(), R.mipmap.ic_grandfather, ivGrandfa1);
            if (!StringUtil.isEmpty(grandFatherBean1.getType()) && grandFatherBean1.getType().equals("2")) {
                tvGrandfa1Name.setText("未装机");
                tvGrandfa1Name.setTextColor(getResources().getColor(R.color.color_EF382D));
            } else {
                tvGrandfa1Name.setText(StringUtil.isEmpty(grandFatherBean1.getMemberCompanyName()) ? "已邀请" :
                        grandFatherBean1.getMemberCompanyName());
                tvGrandfa1Name.setTextColor(getResources().getColor(R.color.color_12CB77));

            }
            if (isAdmin) {
                if (!StringUtil.isEmpty(grandFatherBean1.getType()) && grandFatherBean1.getType().equals("2")) {
                    tvInviteGrandfa1.setVisibility(View.VISIBLE);
                    tvInviteGrandfa1.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandfa1.setVisibility(View.GONE);
                    tvRemoveGrandfa1.setTag(null);
                } else {
                    tvInviteGrandfa1.setVisibility(View.GONE);
                    tvInviteGrandfa1.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandfa1.setVisibility(mSelectedBaby.getRelationshipName().equals("爷爷") ? View.GONE :
                            View.VISIBLE);
                    tvRemoveGrandfa1.setTag(grandFatherBean1);
                }
            } else {
                tvInviteGrandfa1.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandfa1.setVisibility(View.GONE);
                tvRemoveGrandfa1.setVisibility(View.GONE);
            }
        } else {
            if (isAdmin) {
                tvInviteGrandfa1.setBackgroundResource(R.drawable.bg_invite_enable);
                tvInviteGrandfa1.setVisibility(View.VISIBLE);
            } else {
                tvInviteGrandfa1.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandfa1.setVisibility(View.VISIBLE);
            }
            tvGrandfa1Name.setText("未邀请");
            tvGrandfa1Name.setTextColor(getResources().getColor(R.color.color_999999));
            ivGrandfa1.setImageResource(R.mipmap.ic_grandfather);
            tvRemoveGrandfa1.setVisibility(View.GONE);
            tvRemoveGrandfa1.setTag(null);
        }

        RelativeFamilyEntity.ListBean grandMatherBean1 = relativeMap.get("奶奶");
        if (grandMatherBean1 != null) {
            loadImageAvator(grandMatherBean1.getHeadImage(), R.mipmap.ic_grandmother, ivGrandma1);
            if (!StringUtil.isEmpty(grandMatherBean1.getType()) && grandMatherBean1.getType().equals("2")) {
                tvGrandma1Name.setText("未装机");
                tvGrandma1Name.setTextColor(getResources().getColor(R.color.color_EF382D));
            } else {
                tvGrandma1Name.setTextColor(getResources().getColor(R.color.color_12CB77));
                tvGrandma1Name.setText(StringUtil.isEmpty(grandMatherBean1.getMemberCompanyName()) ? "已邀请" :
                        grandMatherBean1.getMemberCompanyName());
            }
            if (isAdmin) {
                if (!StringUtil.isEmpty(grandMatherBean1.getType()) && grandMatherBean1.getType().equals("2")) {
                    tvInviteGrandma1.setVisibility(View.VISIBLE);
                    tvInviteGrandma1.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandma1.setVisibility(View.GONE);
                    tvRemoveGrandma1.setTag(null);
                } else {
                    tvInviteGrandma1.setVisibility(View.GONE);
                    tvInviteGrandma1.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandma1.setVisibility(mSelectedBaby.getRelationshipName().equals("奶奶") ? View.GONE :
                            View.VISIBLE);
                    tvRemoveGrandma1.setTag(grandMatherBean1);
                }
            } else {
                tvInviteGrandma1.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandma1.setVisibility(View.GONE);
                tvRemoveGrandma1.setVisibility(View.GONE);
            }
        } else {
            if (isAdmin) {
                tvInviteGrandma1.setBackgroundResource(R.drawable.bg_invite_enable);
                tvInviteGrandma1.setVisibility(View.VISIBLE);
            } else {
                tvInviteGrandma1.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandma1.setVisibility(View.VISIBLE);
            }
            tvGrandma1Name.setText("未邀请");
            tvGrandma1Name.setTextColor(getResources().getColor(R.color.color_999999));

            ivGrandma1.setImageResource(R.mipmap.ic_grandmother);
            tvRemoveGrandma1.setVisibility(View.GONE);
            tvRemoveGrandma1.setTag(null);
        }


        RelativeFamilyEntity.ListBean grandFatherBean2 = relativeMap.get("姥爷");
        if (grandFatherBean2 != null) {
            loadImageAvator(grandFatherBean2.getHeadImage(), R.mipmap.ic_grandfa, ivGrandfa2);
            if (!StringUtil.isEmpty(grandFatherBean2.getType()) && grandFatherBean2.getType().equals("2")) {
                tvGrandfa2Name.setText("未装机");
                tvGrandfa2Name.setTextColor(getResources().getColor(R.color.color_EF382D));
            } else {
                tvGrandfa2Name.setText(StringUtil.isEmpty(grandFatherBean2.getMemberCompanyName()) ? "已邀请" :
                        grandFatherBean2.getMemberCompanyName());
                tvGrandfa2Name.setTextColor(getResources().getColor(R.color.color_12CB77));
            }
            if (isAdmin) {
                if (!StringUtil.isEmpty(grandFatherBean2.getType()) && grandFatherBean2.getType().equals("2")) {
                    tvInviteGrandfa2.setVisibility(View.VISIBLE);
                    tvInviteGrandfa2.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandfa2.setVisibility(View.GONE);
                    tvRemoveGrandfa2.setTag(null);
                } else {
                    tvInviteGrandfa2.setVisibility(View.GONE);
                    tvInviteGrandfa2.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandfa2.setVisibility(mSelectedBaby.getRelationshipName().equals("姥爷") ? View.GONE :
                            View.VISIBLE);
                    tvRemoveGrandfa2.setTag(grandFatherBean2);
                }
            } else {
                tvInviteGrandfa2.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandfa2.setVisibility(View.GONE);
                tvRemoveGrandfa2.setVisibility(View.GONE);
            }
        } else {
            if (isAdmin) {
                tvInviteGrandfa2.setBackgroundResource(R.drawable.bg_invite_enable);
                tvInviteGrandfa2.setVisibility(View.VISIBLE);
            } else {
                tvInviteGrandfa2.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandfa2.setVisibility(View.VISIBLE);
            }
            tvGrandfa2Name.setText("未邀请");
            tvGrandfa2Name.setTextColor(getResources().getColor(R.color.color_999999));
            ivGrandfa2.setImageResource(R.mipmap.ic_grandfa);
            tvRemoveGrandfa2.setVisibility(View.GONE);
            tvRemoveGrandfa2.setTag(null);
        }


        RelativeFamilyEntity.ListBean grandMatherBean2 = relativeMap.get("姥姥");
        if (grandMatherBean2 != null) {
            loadImageAvator(grandMatherBean2.getHeadImage(), R.mipmap.ic_grandmother, ivGrandma1);
            if (!StringUtil.isEmpty(grandMatherBean2.getType()) && grandMatherBean2.getType().equals("2")) {
                tvGrandma2Name.setText("未装机");
                tvGrandma2Name.setTextColor(getResources().getColor(R.color.color_EF382D));

            } else {
                tvGrandma2Name.setTextColor(getResources().getColor(R.color.color_12CB77));
                tvGrandma2Name.setText(StringUtil.isEmpty(grandMatherBean2.getMemberCompanyName()) ? "已邀请" :
                        grandMatherBean2.getMemberCompanyName());
            }
            if (isAdmin) {
                if (!StringUtil.isEmpty(grandMatherBean2.getType()) && grandMatherBean2.getType().equals("2")) {
                    tvInviteGrandma2.setVisibility(View.VISIBLE);
                    tvInviteGrandma2.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandma2.setVisibility(View.GONE);
                    tvRemoveGrandma2.setTag(null);
                } else {
                    tvInviteGrandma2.setVisibility(View.GONE);
                    tvInviteGrandma2.setBackgroundResource(R.drawable.bg_invite_enable);
                    tvRemoveGrandma2.setVisibility(mSelectedBaby.getRelationshipName().equals("姥姥") ? View.GONE :
                            View.VISIBLE);
                    tvRemoveGrandma2.setTag(grandMatherBean2);
                }
            } else {
                tvInviteGrandma2.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandma2.setVisibility(View.GONE);
                tvRemoveGrandma2.setVisibility(View.GONE);
            }
        } else {
            if (isAdmin) {
                tvInviteGrandma2.setBackgroundResource(R.drawable.bg_invite_enable);
                tvInviteGrandma2.setVisibility(View.VISIBLE);
            } else {
                tvInviteGrandma2.setBackgroundResource(R.drawable.bg_invite_unable);
                tvInviteGrandma2.setVisibility(View.VISIBLE);
            }
            tvGrandma2Name.setText("未邀请");
            tvGrandma2Name.setTextColor(getResources().getColor(R.color.color_999999));
            ivGrandma2.setImageResource(R.mipmap.ic_grandma);
            tvRemoveGrandma2.setVisibility(View.GONE);
            tvRemoveGrandma2.setTag(null);
        }

    }


    private void loadImageAvator(String imageUrl, int defaultImage, ImageView ivAvator) {
        Glide.with(mContext)
                .load(imageUrl)
                .transform(new GlideCircleTransform(mContext))
                .transition(new DrawableTransitionOptions().crossFade())
                .placeholder(defaultImage)
                .error(defaultImage)
                .into(ivAvator);
    }

    private void removeRelative(final RelativeFamilyEntity.ListBean listBean) {
        android.app.AlertDialog dialog = new android.app.AlertDialog.Builder(mContext)
                .setMessage("您确定要移除吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.removeRelative(mSelectedBaby.getId(), listBean.getRelationshipId(),
                                listBean.getMemberCompanyId());
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();

        dialog.show();
    }

    private void initBindRelativeDialog() {
        bindRelativeDialog = new AlertDialog.Builder(mContext)
                .setContentView(R.layout.dialog_bind_relative)
                .setCancelableOntheOutside(true)
                .create();
        RecyclerView rvRelativeList = bindRelativeDialog.getView(R.id.rv_relative);
        mAdapter = new ADA_SelectRelation(mContext);
        rvRelativeList.setLayoutManager(new GridLayoutManager(mContext, 3));
        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                mSelectedBaby.setRelationshipId(mAdapter.getDataList().get(position).getLabel());
                mPresenter.addRelative(mSelectedBaby.getId(),
                        mAdapter.getDataList().get(position).getLabel());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        rvRelativeList.setAdapter(mAdapter);
    }


    private void initInviteDialog() {
        inviteDialog = new AlertDialog.Builder(mContext)
                .setContentView(R.layout.dialog_invite)
                .setWidthAndHeight(mScreenWidth - DisplayUtil.dip2px(mContext, 20), ViewGroup.LayoutParams.WRAP_CONTENT)
                .setCancelableOntheOutside(true)
                .create();
        Window window = inviteDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        inviteDialog.getView(R.id.iv_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inviteByWechat((String) mTvInviteTitle.getTag());
                inviteDialog.dismiss();
            }
        });
        inviteDialog.getView(R.id.iv_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ACT_PhoneInvite.startAc(mContext, mSelectedBaby.getId(), (String) mTvInviteTitle.getTag());
                inviteDialog.dismiss();
            }
        });
        inviteDialog.getView(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inviteDialog.dismiss();
            }
        });
        mTvInviteTitle = inviteDialog.getView(R.id.tv_invite_title);
    }

    private void initBabyListDialog() {


        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_babylist, null);
        babyListDialog = new PopupWindow(mContext);
        babyListDialog.setContentView(view);
        babyListDialog.setWidth(mScreenWidth);
        babyListDialog.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        babyListDialog.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_FFFFFF)));

        RecyclerView rvBabyList = view.findViewById(R.id.rv_babylist);
        rvBabyList.setLayoutManager(new LinearLayoutManager(mContext));
        mBabyAdapter = new ADA_BabyList(mContext);
        rvBabyList.setAdapter(mBabyAdapter);
        mBabyAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                setCheckedItem(position);
                babyListDialog.dismiss();
                viewPopBg.setVisibility(View.GONE);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    private void setCheckedItem(int position) {
        for (int i = 0; i < mBabyAdapter.getDataList().size(); i++) {
            MemberBabyEntity.ListBean babyEntity = mBabyAdapter.getDataList().get(i);
            if (i == position && babyEntity.isChecked()) {
                return;
            }
            babyEntity.setChecked(i == position);
        }
        mBabyAdapter.notifyDataSetChanged();
        mSelectedBaby = mBabyAdapter.getDataList().get(position);
        isAdmin = mSelectedBaby.isAdmin();
        tvTitle.setText(String.format("%s的亲友团", mSelectedBaby.getName()));

        if (com.egg.ylt.Utils.StringUtil.isEmpty(mSelectedBaby.getRelationshipId())) {
            bindRelativeDialog.show();
            return;
        }

        mPresenter.loadFamilyInfo(mSelectedBaby.getId());
    }


    private void inviteByWechat(String relationName) {

        UMWeb web = new UMWeb(String.format("%s?token=%s&companyId=%s&babyId=%s&time=%s&name=%s&imgUrl=%s" +
                        "&relationName=%s&invitedRelationId=%s",
                API.INVITE_URI, Constants.TOKEN, Constants.COMPANYID, mSelectedBaby.getId(), System.currentTimeMillis(),
                mSelectedBaby.getName(), mSelectedBaby.getImageUrl(), relationName,
                mSelectedBaby.getRelationshipId()));//分享链接
        web.setTitle("宝宝的照片都在这里，快来看看吧!");//标题
        web.setDescription("来婴联通，全家的照片都在这");//描述

        new ShareAction(mContext)
                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                .withMedia(web)//分享链接
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        ToastUtil.makeText(mContext, "邀请成功", false);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        ToastUtil.makeText(mContext, "邀请失败", false);
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {

                    }
                })//回调监听器
                .share();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mSelectedBaby != null) {
            mPresenter.loadFamilyInfo(mSelectedBaby.getId());
        }
    }

    public static void startAc(Context context) {
        Intent intent = new Intent(context, ACT_Family.class);
        context.startActivity(intent);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (inviteDialog != null && inviteDialog.isShowing()) {
            inviteDialog.dismiss();
        }
        if (babyListDialog != null && babyListDialog.isShowing()) {
            babyListDialog.dismiss();
        }
        if (bindRelativeDialog != null && bindRelativeDialog.isShowing()) {
            bindRelativeDialog.dismiss();
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_family;
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

}
